package org.cncy;

import org.apache.http.entity.StringEntity;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.HashMap;

public class DeliveryPolicySampler extends AbstractJavaSamplerClient implements Serializable {

    private static  final Logger log = LoggerFactory.getLogger(DeliveryPolicySampler.class);
    private static final long serialVersionUID = 240l;

    private final static String AUTHORIZATION = "Authorization";
    private final static String DEVICEID = "deviceId";
    private final static String URL = "url";

    public DeliveryPolicySampler() {
        log.debug(Utils.whoAmI(this) + "\tConstruct");
    }


    //通过jmter的图形框进行传参,无默认值
    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.setComment("下发简单策略:锁定设备");
        arguments.addArgument(AUTHORIZATION, "");
        arguments.addArgument(DEVICEID, "");
        arguments.addArgument(URL, "");
        return arguments;
    }

    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult results = new SampleResult();
        //返回请求值，体现在jmeter查看结果数请求里面
        String deviceId = javaSamplerContext.getParameter(DEVICEID);
        String autho = javaSamplerContext.getParameter(AUTHORIZATION);
        String url = javaSamplerContext.getParameter(URL);
        log.debug("deviceId = {}, auth = {}, url = {}", deviceId, autho, url);
        String body = "{\"lockOp\":\"LOCK\",\"lockPassword\":\"5MJRqBq3skfTj0KNqMsFo/zaQE0FATAMTQEQJy/2l8n8x8Ndzh+sGTyQIzOfvYpU2PlLqz5aqFAf3SZEEYR6Ti5HmpkgTaqXJWFwDq84Satz45FEp80sbMybwFIfRrYwgGQ4ve5ztrgleIOOtlwjRF4GYw4r7axV62wavLiTTEE=\",\"deviceIds\":\"" + deviceId + "\"}";
        results.setSamplerData("请求体 : " + body);
        results.sampleStart();
        try {
            StringEntity stringEntity = new StringEntity(body);
            HashMap<String, String> header = new HashMap<>();
            header.put("Content-Type", "application/json;charset=UTF-8");
            header.put(AUTHORIZATION, autho);
            String strBody = HttpsUtils.sendData(stringEntity, url, header);
            // TODO: 2023/12/12 这个接口返回空字符代表成功， 如果返回null 或其他就是失败
            log.debug("response = {}", strBody);
            results.setResponseData(strBody, "utf-8");
            if (!"".equals(strBody)) {
                throw new Exception("下发策略失败，请排查原因");
            }
            //这里决定测试用例请求成功还是失败，最总结果还有含是否有其他断言等
            results.setSuccessful(true);
            // 设置返回信息
            results.setResponseCode("200");
            results.setResponseMessage("下发策略成功");
            //如果失败 samplerlabel 不生效
            //results.setSampleLabel("下发策略");
        } catch (Exception e) {
            results.setSuccessful(false);
            // Record end time and populate the results.
            results.sampleEnd();
        }
        if (log.isDebugEnabled()) {
            log.debug(Utils.whoAmI(this) + "\tTime:\t" + results.getTime());
        }

        return results;
    }

}
